
class AliLogTracker {
  params_: string[];
  uri_: string;
  constructor(host: string, project: string, logstore: string) {
    this.uri_ = 'https://' + project + '.' + host + '/logstores/' + logstore + '/track?APIVersion=0.6.0';
    this.params_ = new Array();
  }
  push(key: string, value: any) {
    if (!key) {
      return;
    }
    if (value === '') {
      return;
    }
    this.params_.push(key);
    this.params_.push(value);
  }

  logger() {
    let url = this.uri_;
    this.params_.forEach((v, k) => {
      if (k % 2 == 0) {
        url += '&' + encodeURIComponent(v);
      } else {
        url += '=' + encodeURIComponent(v);
      }
    })
    try {
      fetch(url)
    } catch (e: any) {
      if (window && window.console && typeof window.console.log === 'function') {
        console.log("Failed to log to ali log service because of this exception:\n" + e);
        console.log("Failed log data", url);
      }
    }
  }
}

export default AliLogTracker;